package com.neusoft.elmboot.mapper;

import com.neusoft.elmboot.po.dto.UserDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.neusoft.elmboot.po.User;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper {

	@Select("select * from user where userId=#{userId} and password=#{password}")
	public User getUserByIdByPass(User user);
	
	@Select("select count(*) from user where userId=#{userId}")
	public int getUserById(String userId);
	
	@Insert("insert into user (userId, password, userName, userSex, userImg, delTag, role, points, experiencePoints, level, discount, isSuperMember, superMemberExpireTime) values(#{userId},#{password},#{userName},#{userSex},null,1,2,0,0,1,99,0,null)")
	public int saveUser(User user);

	@Select("select password from user where userId = #{userId}")
    String getPasswordById(String userId);

	@Update("update user set password = #{newPassword} where userId = #{userId}")
	void changePassword(String userId, String newPassword);

	@Select("select * from user where userId = #{userId}")
    User getUserInfoById(String userId);

	@Update("update user set userName = #{userName}, userImg = #{userImg} where userId = #{userId}")
    void updateInfo(UserDTO userDTO);
	
	@Update("update user set points = #{points}, experiencePoints = #{experiencePoints} where userId = #{userId}")
	void updatePointsAndXP(String userId, int points, int experiencePoints);
	
	@Update("update user set level = #{level}, discount = #{discount} where userId = #{userId}")
	void updateLevelAndDiscount(String userId, int level, int discount);
	
	@Update("update user set points = #{points} where userId = #{userId}")
	void updatePoints(String userId, int points);
	
	@Update("update user set isSuperMember = #{isSuperMember}, superMemberExpireTime = #{expireTime} where userId = #{userId}")
	void updateSuperMemberStatus(String userId, int isSuperMember, String expireTime);

	// ==================== 管理员功能 ====================

	/**
	 * 获取用户总数
	 */
	@Select("SELECT COUNT(*) FROM user")
	int getUserCount();

	/**
	 * 获取今日新增用户数（由于没有注册时间字段，返回0）
	 */
	@Select("SELECT 0")
	int getTodayUserCount();

	/**
	 * 获取活跃用户数
	 */
	@Select("SELECT COUNT(*) FROM user WHERE delTag = 1")
	int getActiveUserCount();

	/**
	 * 分页获取用户列表
	 */
	@Select("SELECT userId, userName, userSex, delTag, level, points, experiencePoints " +
			"FROM user ORDER BY userId DESC LIMIT #{size} OFFSET #{offset}")
	java.util.List<User> getUsersByPage(@org.apache.ibatis.annotations.Param("size") Integer size, 
	                                   @org.apache.ibatis.annotations.Param("offset") Integer offset);

	/**
	 * 搜索用户
	 */
	@Select("SELECT userId, userName, userSex, delTag, level, points, experiencePoints " +
			"FROM user WHERE userName LIKE CONCAT('%', #{keyword}, '%') OR userId LIKE CONCAT('%', #{keyword}, '%') " +
			"ORDER BY userId DESC LIMIT #{size} OFFSET #{offset}")
	java.util.List<User> searchUsers(@org.apache.ibatis.annotations.Param("keyword") String keyword,
	                                @org.apache.ibatis.annotations.Param("size") Integer size,
	                                @org.apache.ibatis.annotations.Param("offset") Integer offset);

	/**
	 * 搜索用户总数
	 */
	@Select("SELECT COUNT(*) FROM user WHERE userName LIKE CONCAT('%', #{keyword}, '%') OR userId LIKE CONCAT('%', #{keyword}, '%')")
	int searchUsersCount(String keyword);

	/**
	 * 检查用户是否存在
	 */
	@Select("SELECT COUNT(*) FROM user WHERE userId = #{userId}")
	int checkUserExists(String userId);

	/**
	 * 添加用户（管理员）
	 */
	@Insert("INSERT INTO user (userId, password, userName, userSex, userPhone, level, points, experiencePoints, delTag, role, userRegisterTime) " +
			"VALUES (#{userId}, #{password}, #{userName}, #{userSex}, #{userPhone}, #{level}, #{points}, #{experiencePoints}, 1, 2, NOW())")
	int addUser(User user);

	/**
	 * 更新用户信息（管理员）
	 */
	@Update("UPDATE user SET userName = #{userName}, userSex = #{userSex}, userPhone = #{userPhone}, " +
			"level = #{level}, points = #{points}, experiencePoints = #{experiencePoints} WHERE userId = #{userId}")
	int updateUser(User user);

	/**
	 * 删除用户
	 */
	@Update("UPDATE user SET delTag = 0 WHERE userId = #{userId}")
	int deleteUser(String userId);

	/**
	 * 更新用户状态
	 */
	@Update("UPDATE user SET delTag = #{status} WHERE userId = #{userId}")
	int updateUserStatus(@org.apache.ibatis.annotations.Param("userId") String userId, 
	                    @org.apache.ibatis.annotations.Param("status") Integer status);
}
